﻿using BrightIdeasSoftware;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FormTest
{
    public partial class Form2 : Form
    {
        private ObjectListView _olv;
        private List<FileItem> _fileItems = new List<FileItem>();
        public Form2()
        {
            InitializeComponent();

             InitializeObjectListView();
             LoadSampleData();
        }

        // 初始化ObjectListView
        private void InitializeObjectListView()
        {
            // 创建控件并设置基本属性
            _olv = new ObjectListView
            {
                Dock = DockStyle.Fill,
                View = View.Details,
                FullRowSelect = true,
                GridLines = true,
                //AllowColumnReordering = true, // 允许列重排
                VirtualMode = true, // 启用虚拟模式（大数据优化）
                RowHeight = 24 // 行高
            };

            // 定义列并绑定到对象属性
            _olv.Columns.AddRange(new OLVColumn[]
            {
                // 名称列（带图标）
                new OLVColumn("名称", "Name")
                {
                    Width = 250,
                    ImageGetter = item => ((FileItem)item).IsDirectory ? 0 : 1
                },
                // 扩展名列
                new OLVColumn("扩展名", "Extension") { Width = 100 },
                // 大小列（自定义格式化）
                new OLVColumn("大小", "Size")
                {
                    Width = 120,
                    AspectToStringConverter = size => FormatSize((long)size)
                },
                // 修改日期列
                new OLVColumn("修改日期", "ModifiedDate")
                {
                    Width = 180,
                    AspectToStringConverter = date => ((DateTime)date).ToString("yyyy-MM-dd HH:mm")
                }
            });

            // 添加图标列表
            var imageList = new ImageList { ImageSize = new Size(32, 32) };
            imageList.Images.SetKeyName(0, "2025-07-21_110922.png");
            imageList.Images.SetKeyName(1, "2025-07-25_113601.png");
            imageList.Images.SetKeyName(2, "控制器联网2.drawio.png");
            //imageList.Images.Add(SystemIcons.Warning.ToBitmap()); // 索引0：文件夹图标
            //imageList.Images.Add(SystemIcons.Question.ToBitmap());   // 索引1：文件图标
            _olv.SmallImageList = imageList;
            //_olv.SmallImageList = imageList;
            // 启用排序
            _olv.UseAlternatingBackColors = true; // 交替行颜色
            _olv.HeaderWordWrap = true;

            // 添加事件（双击打开）
            _olv.DoubleClick += (s, e) =>
            {
                var selected = _olv.SelectedObject as FileItem;
                if (selected != null)
                {
                    MessageBox.Show($"打开: {selected.Name}");
                }
            };

            // 添加到窗体
            Controls.Add(_olv);
        }

        // 加载示例数据
        private void LoadSampleData()
        {
            // 模拟文件数据
            _fileItems = new List<FileItem>
            {
                new FileItem {
                    Name = "文档",
                    Extension = "",
                    IsDirectory = true,
                    ModifiedDate = new DateTime(2023, 10, 1)
                },
                new FileItem {
                    Name = "报告",
                    Extension = ".docx",
                    Size = 204800,
                    ModifiedDate = new DateTime(2023, 10, 5)
                },
                new FileItem {
                    Name = "截图",
                    Extension = ".png",
                    Size = 1536000,
                    ModifiedDate = new DateTime(2023, 10, 3)
                },
                new FileItem {
                    Name = "笔记",
                    Extension = ".txt",
                    Size = 1280,
                    ModifiedDate = new DateTime(2023, 10, 8)
                }
            };
            //virtualObjectListView1.VirtualListSize = _fileItems.Count;
            //virtualObjectListView1.SetObjects( _fileItems);
            virtualObjectListView1.AddObjects( _fileItems );
            // 绑定数据
            //_olv.SetObjects(_fileItems);
        }
        // 格式化文件大小（字节 → KB/MB）
        private string FormatSize(long bytes)
        {
            if (bytes < 1024)
                return $"{bytes} B";
            else if (bytes < 1024 * 1024)
                return $"{bytes / 1024.0:F1} KB";
            else
                return $"{bytes / (1024.0 * 1024):F1} MB";
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }
    }
    // 示例数据模型
    public class FileItem
    {
        public string Name { get; set; }
        public string Extension { get; set; }
        public long Size { get; set; } // 字节
        public DateTime ModifiedDate { get; set; }
        public bool IsDirectory { get; set; }
    }
}
